package org.shaka.tools;

public class StringTools {

    public static String camel2dash(String str) {
        return camel2dash(str, false);
    }

    public static String camel2dash(String str, boolean isUpperCase) {
        if (str == null)
            return null;
        if (str.length() == 0)
            return str;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0 && Character.isUpperCase(c)) {
                sb.append('-');
            }
            sb.append(isUpperCase ? Character.toUpperCase(c) : Character.toLowerCase(c));
        }
        return sb.toString();
    }

    public static String dash2camel(String str) {
        return dash2camel(str, false);
    }

    public static String dash2camel(String str, boolean isFirstToUpperCase) {
        if (str == null)
            return null;
        if (str.length() == 0)
            return str;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = Character.toLowerCase(str.charAt(i));
            if (isFirstToUpperCase) {
                c = Character.toUpperCase(c);
                isFirstToUpperCase = false;
            }
            if (c == '-') {
                isFirstToUpperCase = true;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String rightPad(String str, int length) {
        return padString(str, ' ', true, length);
    }

    public static String leftPad(String str, int length) {
        return padString(str, ' ', false, length);
    }

    private static String padString(String str, char padChar, boolean isRight, int length) {
        str = (str == null ? "" : str);
        int strLength = str.replaceAll("[^\\x00-\\x7F]", "__").length();
        StringBuilder result = new StringBuilder();
        if (isRight) {
            result.append(str);
        }
        for (int i = 0; i < (length - strLength); i++) {
            result.append(padChar);
        }
        if (!isRight) {
            result.append(str);
        }
        return result.toString();
    }
}
